
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>this · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Symbol和generator.html" />
    
    
    <link rel="prev" href="class和继承.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="变量.html">
            
                <a href="变量.html">
            
                    
                    变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="解构.html">
            
                <a href="解构.html">
            
                    
                    解构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="prototype.html">
            
                <a href="prototype.html">
            
                    
                    prototype
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="模块化.html">
            
                <a href="模块化.html">
            
                    
                    模块化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="class和继承.html">
            
                <a href="class和继承.html">
            
                    
                    class和继承
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.12" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="Symbol和generator.html">
            
                <a href="Symbol和generator.html">
            
                    
                    Symbol和generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="async_await.html">
            
                <a href="async_await.html">
            
                    
                    async_await
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Set和WeakSet.html">
            
                <a href="Set和WeakSet.html">
            
                    
                    Set和WeakSet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Map和WeakMap.html">
            
                <a href="Map和WeakMap.html">
            
                    
                    Map和WeakMap
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="数字和Math.html">
            
                <a href="数字和Math.html">
            
                    
                    数字和Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="正则.html">
            
                <a href="正则.html">
            
                    
                    正则
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="Proxy.html">
            
                <a href="Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >this</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="this">this</h1>
<p>ref:<br><a href="https://www.jianshu.com/p/c1ee12a328d2/" target="_blank">https://www.jianshu.com/p/c1ee12a328d2/</a></p>
<h3 id="&#x666E;&#x901A;&#x51FD;&#x6570;&#x4E2D;&#x7684;this">&#x666E;&#x901A;&#x51FD;&#x6570;&#x4E2D;&#x7684;this</h3>
<p>this&#x5BF9;&#x8C61;&#x662F;&#x5728;&#x8FD0;&#x884C;&#x65F6;&#x57FA;&#x4E8E;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x73AF;&#x5883;&#x7ED1;&#x5B9A;&#x7684;&#xFF1A;&#x5728;&#x5168;&#x5C40;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;this&#x6307;&#x5411;&#x7684;&#x662F;window&#xFF1B;&#x5F53;&#x51FD;&#x6570;&#x88AB;&#x4F5C;&#x4E3A;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;&#x8C03;&#x7528;&#x65F6;&#xFF0C;this&#x5C31;&#x7B49;&#x4E8E;&#x90A3;&#x4E2A;&#x5BF9;&#x8C61;, this&#x662F;&#x53EA;&#x5728;&#x5F53;&#x524D;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x4F7F;&#x7528;&#x7684;</p>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    var name = &quot;the window&quot;;
    var object = {
        name: &apos;my object&apos;,
        getNameFun: function() {
            return this.name; // &#x8FD9;&#x91CC;this&#x662F;object&#x5BF9;&#x8C61;&#x672C;&#x8EAB;
        }
    }
    console.log(object.getNameFun()); // my object
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    var name = &quot;the window&quot;;
    var object = {
        name: &apos;my object&apos;,
        getNameFun: function() {
            return function() { //&#x533F;&#x540D;&#x51FD;&#x6570;
                return this.name;
                //&#x533F;&#x540D;&#x51FD;&#x6570;&#x7684;&#x6267;&#x884C;&#x73AF;&#x5883;&#x662F;&#x5168;&#x5C40;&#x7684;, &#x8FD9;&#x91CC;&#x7684;this&#x662F;window
                    //this&#x53EA;&#x5728;&#x51FD;&#x6570;&#x5185;&#x90E8;&#x8D77;&#x4F5C;&#x7528;,&#x8FD9;&#x91CC;this.name&#x4F1A;&#x5728;&#x8FD9;&#x4E2A;&#x533F;&#x540D;&#x51FD;&#x6570;&#x4E2D;&#x627E;name,&#x4F46;&#x662F;&#x6CA1;&#x6709;&#x627E;&#x5230;
                    //&#x4E8E;&#x662F;&#x5C31;&#x4ECE;&#x5168;&#x5C40;&#x4E2D;&#x627E;
            }
        }
    }
    console.log(object.getNameFun()()); // the window
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    var name = &quot;the window&quot;;
    var object = {
        name: &apos;my object&apos;,
        getNameFun: function() {
            var that = this; // &#x8FD9;&#x91CC;&#x7684;this&#x662F;&#x6302;&#x8F7D;&#xFF08;&#x8C03;&#x7528;&#xFF09;&#x7684;&#x5BF9;&#x8C61;object, that&#x4E5F;&#x5C31;&#x662F;object
            return function() {
                return that.name; //that.name&#x4F1A;&#x5728;object&#x4E0A;&#x67E5;&#x627E;&#x76F8;&#x5E94;&#x7684;&#x6570;&#x636E;
            }
        }
    }
    console.log(object.getNameFun()()); // my object
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    let object = {
        func: function() {
            console.log(this);    
        },
        say: function() {
            let that = this;
            setTimeout(function() {
                console.log(that);
            })
        }
    };

    object.func(); // object
    object.say(); // object
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    window.val = 1;
    var object = {
        val: 2,
        db1: function() {
            //console.log(this); // object
            this.val *= 2;
            val *= 2;
            console.log(val);
            console.log(this.val);
        }
    };

    object.db1(); 
    // 2 4 &#x5F53;&#x6267;&#x884C;object.db1()&#x65F6;, this&#x4EE3;&#x8868;object, &#x4E8E;&#x662F;: window.val *= 2 &#x53D8;&#x6210;&#x4E86; 2, this.val *=2 &#x53D8;&#x6210;&#x4E86; 4
    var func = object.db1;
    func(); 
    // &#x8FD9;&#x4E00;&#x53E5;&#x6267;&#x884C;&#x65F6;,&#x76F8;&#x5F53;&#x4E8E;window.func(), &#x4E8E;&#x662F; this &#x4EE3;&#x8868;&#x4E86; window
    // &#x4E8E;&#x662F; window.val &#x7ECF;&#x8FC7;&#x4E24;&#x6B21;*2 &#x53D8;&#x6210;&#x4E86; 8
&lt;/script&gt;

// &#x6700;&#x7EC8;&#x8F93;&#x51FA;: 2 4 8 8
</code></pre>
<h3 id="es6&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x7684;this">es6&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x7684;this</h3>
<p>&#x7BAD;&#x5934;&#x51FD;&#x6570;&#x7684;this&#x662F;&#x5728;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x65F6;&#x7ED1;&#x5B9A;&#x7684;&#xFF0C;&#x4E0D;&#x662F;&#x5728;&#x6267;&#x884C;&#x8FC7;&#x7A0B;&#x4E2D;&#x7ED1;&#x5B9A;&#x7684;&#x3002;&#x7B80;&#x5355;&#x7684;&#x8BF4;&#xFF0C;&#x51FD;&#x6570;&#x5728;&#x5B9A;&#x4E49;&#x65F6;&#xFF0C;this&#x5C31;&#x7EE7;&#x627F;&#x4E86;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x7684;&#x5BF9;&#x8C61;&#x3002;
&#x5373;this&#x6307;&#x5B9A;&#x4E49;&#x51FD;&#x6570;&#x65F6;&#x6240;&#x5728;&#x7684;&#x5BF9;&#x8C61;</p>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    var object = {
        func: function() {
            console.log(this);
        },
        say: function() {
            setTimeout(()=&gt;{
                console.log(this);
            })
        }
    }

    object.func(); //object
    object.say(); //object
&lt;/script&gt;
</code></pre>
<pre><code class="lang-js">&lt;script type=&quot;text/javascript&quot;&gt;
    var a = 11;
    function test() {
        this.a = 22;
        return ()=&gt;{
            console.log(this.a);
        }
    }
    test()(); //22
&lt;/script&gt;
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="class和继承.html" class="navigation navigation-prev " aria-label="Previous page: class和继承">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Symbol和generator.html" class="navigation navigation-next " aria-label="Next page: Symbol和generator">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"this","level":"1.12","depth":1,"next":{"title":"Symbol和generator","level":"1.13","depth":1,"path":"Symbol和generator.md","ref":"Symbol和generator.md","articles":[]},"previous":{"title":"class和继承","level":"1.11","depth":1,"path":"class和继承.md","ref":"class和继承.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"this.md","mtime":"2021-11-29T15:25:33.393Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-02-17T15:54:39.156Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

